package com.tedu.weibotest.controller;


import com.tedu.weibotest.mapper.WeiboMapper;
import com.tedu.weibotest.pojo.dto.WeiboDTO;
import com.tedu.weibotest.pojo.entity.User;
import com.tedu.weibotest.pojo.entity.Weibo;
import com.tedu.weibotest.pojo.vo.WeiboDetailVO;
import com.tedu.weibotest.pojo.vo.WeiboIndexVO;
import com.tedu.weibotest.pojo.vo.WeiboListCommentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;


    // 发布微博 状态码 0:失败 1:成功 2:未登录
    @PostMapping("/insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        User user=(User) session.getAttribute("user");
        if (user!=null){
            Weibo weibo=new Weibo();
            BeanUtils.copyProperties(weiboDTO,weibo);
            weibo.setCreated(new Date());
            weibo.setUserId(user.getId());
            int row=weiboMapper.insert(weibo);
            return row>0?1:0;
        }else {
            return 2;
        }
    }

    // 查询微博列表
    @GetMapping("/selectIndex")
    public List<WeiboIndexVO> selectIndex(){
        return weiboMapper.selectIndexWeibo();
    }

    // 查询微博详情
    @GetMapping("/selectById")
    public WeiboDetailVO selectById(Integer id){
        return weiboMapper.selectDetailWeiboById(id);

    }

    // 获取微博 和评论列表
    @GetMapping("/selectWeiboAndComment")
    public List<WeiboListCommentVo> selectWeiboAndComment(){
        return weiboMapper.selectWeiboComment();
    }

}
